﻿using SimpleUnitity.Database.Extension;
using SimpleUnitity.Log;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;

namespace SimpleUnitity.Database.Config
{
    public class DbProviderSection : ConfigurationSection
    {
        [ConfigurationProperty("Name", IsRequired = true)]
        public string Name
        {
            get { return (string)base["Name"]; }
            set { base["Name"] = value; }
        }

        [ConfigurationProperty("ConnectionClass", IsRequired = true)]
        public string ConnectionClass
        {
            get { return (string)base["ConnectionClass"]; }
            set { base["ConnectionClass"] = value; }
        }

        [ConfigurationProperty("DataAdapterClass", IsRequired = true)]
        public string DataAdapterClass
        {
            get { return (string)base["DataAdapterClass"]; }
            set { base["DataAdapterClass"] = value; }
        }

        [ConfigurationProperty("ParameterPrefix", IsRequired = false, DefaultValue = "@")]
        public string ParameterPrefix
        {
            get { return (string)base["ParameterPrefix"]; }
            set { base["ParameterPrefix"] = value; }
        }

        [ConfigurationProperty("StatementPrefix", IsRequired = false, DefaultValue = "[")]
        public string StatementPrefix
        {
            get { return (string)base["StatementPrefix"]; }
            set { base["StatementPrefix"] = value; }
        }

        [ConfigurationProperty("StatementSuffix", IsRequired = false, DefaultValue = "]")]
        public string StatementSuffix
        {
            get { return (string)base["StatementSuffix"]; }
            set { base["StatementSuffix"] = value; }
        }

        [ConfigurationProperty("StatementSymbolAlias", IsRequired = false, DefaultValue = "")]
        public string StatementSymbolAlias
        {
            get { return (string)base["StatementSymbolAlias"]; }
            set { base["StatementSymbolAlias"] = value; }
        }

        [ConfigurationProperty("IdentitySQL", IsRequired = false)]
        public string IdentitySQL
        {
            get { return (string)base["IdentitySQL"]; }
            set { base["IdentitySQL"] = value; }
        }

        private Type connectionType;
        private Type dataAdapterType;

        public IDbConnection GetConnection()
        {
            if (connectionType == null)
            {
                connectionType = TypeLoader.Get(ConnectionClass);
            }
            return (IDbConnection)Activator.CreateInstance(connectionType);
        }

        public IDbDataAdapter GetDataAdapter()
        {
            if (dataAdapterType == null)
            {
                dataAdapterType = TypeLoader.Get(DataAdapterClass);
            }
            return (IDbDataAdapter)Activator.CreateInstance(dataAdapterType);
        }

        public DbTool CreateDbTool(string connectionString)
        {
            var conn = GetConnection();
            conn.ConnectionString = connectionString;
            var adapter = GetDataAdapter();
            return new ProviderDbTool(conn, adapter, ParameterPrefix, StatementPrefix, StatementSuffix)
            {
                IdentitySQL = IdentitySQL,
                StatementSymbolAlias = StatementSymbolAlias
            };
        }

        public static DbProviderSection GetSection(string sectionName)
        {
            return (DbProviderSection)ConfigurationManager.GetSection(sectionName);
        }
    }
}
